package com.zch.innerclass;

/**
 * 匿名内部类
 */
public class AnonymousInnerClass {
    public static void main(String[] args) {
        Outer02 outer02 = new Outer02();
        outer02.method();
    }
}

class Outer02 {
    private int x = 20;
    public void method() {
        //基于接口的匿名内部类
        //cat的运行类型就是匿名内部类XXXX = Outer02$1
        //jdk底层在创建匿名内部类Outer02$1后立马就创建了Outer02$1的实例，并且把地址返回给cat
        /*
            底层：
            class XXXX implements A {
                public void cry() {
                    System.out.println("小猫喵喵叫");
                }
            }
         */
        A cat = new A() {
            public void cry() {
                System.out.println("小猫喵喵叫");
            }
        };
        System.out.println("cat的运行类型：" + cat.getClass()); //getClass方法获取运行类型
        cat.cry();
        cat.cry();
        cat.cry();

        //基于类的匿名内部类
        /*
            底层：
            class Outer02$2 extends Father {
                public void test() {
                    System.out.println("匿名内部类重写了test方法");;
                }
            }
         */
        //参数列表会传递给构造器
        Father father = new Father("jack") {
            @Override
            public void test() {
                System.out.println("匿名内部类重写了test方法");;
            }
        };
        System.out.println("father的运行类型：" + father.getClass());
        father.test();
    }
}

interface A {
    public void cry();
}

class Father {
    public Father(String name) {
        System.out.println("接收到" + name);
    }
    public void test() {

    }
}